package com.csoa.community.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.collect.community.R;
import com.csoa.community.adapter.WZTableAdapter;
import com.csoa.community.bean.WZInfoBean;
import com.csoa.community.bean.YFInfoBean;
import com.csoa.community.db.WZInfoDB;
import com.csoa.community.utils.StringUtils;
import com.csoa.community.widget.PullToRefreshListView;

public class WZInfoTableActivity extends BaseActivity {
	ListView lv;
	private ListView mList;
	public static List<WZInfoBean> mWZInfos;
	private WZTableAdapter adapter;
	private EditText etWZLadinTable;
	private EditText etWZChineseTable;
	private EditText etWZCommonTable;
	private YFInfoBean mYZInfo;
	private PullToRefreshListView mPullToRefreshListView;

	@Override
	public void initUI() {
		mYZInfo = (YFInfoBean) this.getIntent().getSerializableExtra("YFInfo");
		setContentView(R.layout.wz_info_table);
		mPullToRefreshListView = (PullToRefreshListView) this.findViewById(R.id.listview);
		mPullToRefreshListView.setMode(PullToRefreshListView.MODE_NO);
		mList = mPullToRefreshListView.getRefreshableView();
		etWZLadinTable = (EditText)this.findViewById(R.id.etWZLadinTable);
		etWZChineseTable = (EditText)this.findViewById(R.id.etWZChineseTable);
		etWZCommonTable = (EditText)this.findViewById(R.id.etWZCommonTable);
		
		
		this.findViewById(R.id.btnRefreshWZ).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						refresh();
					}
				});
		this.findViewById(R.id.btnBackWZTable).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						setResult(RESULT_OK);
						finish();
					}
				});
		
		this.findViewById(R.id.btnQueryWZ).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						String latinName = etWZLadinTable.getText().toString().trim();
						String chineseName = etWZChineseTable.getText().toString().trim();
						String commonName = etWZCommonTable.getText().toString().trim();
						if(StringUtils.isEmpty(latinName) && StringUtils.isEmpty(chineseName) && StringUtils.isEmpty(commonName)) {
							WZInfoTableActivity.this.showToast("请输入任一搜索条件");
							return;
						}
						mWZInfos.clear();
						List<WZInfoBean> wzInfos = WZInfoDB.query(latinName,chineseName,commonName);
						if (wzInfos == null || wzInfos.size() == 0) {
							WZInfoTableActivity.this.showToast("您搜索的物种信息不存在");
						}else {
							mWZInfos.addAll(wzInfos);
						}
						adapter.notifyDataSetChanged();
					}
				});
		
		this.findViewById(R.id.btnAddWZ).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(WZInfoTableActivity.this, WZInfoActivity.class);
						intent.putExtra("yfId", mYZInfo.id);
						intent.putExtra("which", 1);
						startActivityForResult(intent, 0);
					}
				});
		
	}

	@Override
	public void initData() {
		mWZInfos = new ArrayList<WZInfoBean>();
		List<WZInfoBean> wzInfos = WZInfoDB.query(mYZInfo.id);
		if (wzInfos != null) {
			mWZInfos.addAll(wzInfos);
		}
		adapter = new WZTableAdapter(this, mWZInfos);
		mList.setAdapter(adapter);
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (resultCode == RESULT_OK) {
			
			if(intent != null) {
				WZInfoBean delWZInfo = (WZInfoBean)intent.getSerializableExtra("delete");
				if(delWZInfo != null) {//删除
					int position = intent.getIntExtra("position", 0);
					mWZInfos.remove(position);
				}else {
					WZInfoBean updateWZInfo = (WZInfoBean)intent.getSerializableExtra("update");
					if(updateWZInfo != null) {
						int position = intent.getIntExtra("position", 0);
						if(updateWZInfo != null) {//更新
							mWZInfos.remove(position);
							mWZInfos.add(position, updateWZInfo);
						}
					}else {//添加
						refresh();
						return;
					}
				}
				
			}else {
				refresh();
				return;
			}
			adapter.notifyDataSetChanged();
		}
	}
	
	
	public void refresh() {
		List<WZInfoBean> wzInfos = WZInfoDB.query(mYZInfo.id);
		if(wzInfos != null) {
			mWZInfos.clear();
			mWZInfos.addAll(wzInfos);
			clearEdit();
			adapter.notifyDataSetChanged();
		}
	}
	@Override
	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		if(paramInt == KeyEvent.KEYCODE_BACK) {
			setResult(RESULT_OK);
			finish();
		}
		return super.onKeyDown(paramInt, paramKeyEvent);
	}
	
	public void clearEdit() {
		etWZChineseTable.setText("");
		etWZLadinTable.setText("");
		etWZCommonTable.setText("");
	}

}
